<template>
  <div class="login-container">
    <div class="login-box">
      <!-- 猫头鹰动画部分 -->
      <div class="owl" :class="{ password: isPasswordFocused }">
        <div class="hand"></div>
        <div class="hand hand-r"></div>
        <div class="arms">
          <div class="arm"></div>
          <div class="arm arm-r"></div>
        </div>
      </div>
      
      <div class="input-box">
        <div class="login-header">
          <h1>电信客户流失预警系统</h1>
        </div>
        
        <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" @keyup.enter="handleLogin">
          <el-form-item prop="username">
            <el-input 
              v-model="loginForm.username" 
              placeholder="请输入用户名" 
            >
              <template #prefix>
                <el-icon><User /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input 
              v-model="loginForm.password" 
              placeholder="请输入密码" 
              show-password
              type="password"
              @focus="handlePasswordFocus"
              @blur="handlePasswordBlur"
            >
              <template #prefix>
                <el-icon><Lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-checkbox v-model="loginForm.remember">记住账号</el-checkbox>
          </el-form-item>
          <el-form-item>
            <el-button 
              :loading="loading" 
              type="primary" 
              class="login-button" 
              @click="handleLogin"
            >
              {{ loading ? '登录中...' : '登录' }}
            </el-button>
          </el-form-item>
        </el-form>
        
        <!-- <div class="login-tips">
          <p>默认账号: admin</p>
          <p>默认密码: admin</p>
        </div> -->
      </div>

      <div class="login-footer">
        <p>© {{ new Date().getFullYear() }} 电信客户流失预警系统 - 版权所有</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { useUserStore } from '@/store/modules/user'

// 路由和store
const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

// 表单引用
const loginFormRef = ref(null)
const loading = ref(false)
// 控制猫头鹰眼睛状态
const isPasswordFocused = ref(false)

// 表单数据
const loginForm = reactive({
  username: 'admin',
  password: 'admin',
  remember: false
})

// 表单验证规则
const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ]
}

// 密码框焦点处理
const handlePasswordFocus = () => {
  isPasswordFocused.value = true
}

const handlePasswordBlur = () => {
  isPasswordFocused.value = false
}

// 登录方法
const handleLogin = async () => {
  try {
    // 表单验证
    await loginFormRef.value.validate()
    
    // 显示加载状态
    loading.value = true
    
    // 调用登录接口
    await userStore.login(loginForm)
    
    // 登录成功消息
    ElMessage.success('登录成功')
    
    // 重定向到首页或指定页面
    const redirect = route.query.redirect || '/'
    router.push(redirect)
  } catch (error) {
    console.error('登录失败:', error)
    ElMessage.error(error.message || '登录失败，请检查用户名和密码')
  } finally {
    loading.value = false
  }
}
</script>

<style lang="scss" scoped>
/* 全局样式 */
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f0f2f5;
}

/* 登录框样式 */
.login-box {
  position: relative;
  width: 380px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  padding: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 60px; /* 为猫头鹰预留空间 */
}

.input-box {
  width: 100%;
}

.login-header {
  text-align: center;
  margin-bottom: 20px;

  h1 {
    font-size: 22px;
    color: #303133;
    font-weight: 500;
    margin: 0;
  }
}

.login-button {
  width: 100%;
  border-radius: 4px;
  height: 40px;
  font-size: 16px;
}

.login-tips {
  margin-top: 10px;
  color: #606266;
  font-size: 14px;
  text-align: center;
  
  p {
    margin: 5px 0;
  }
}

.login-footer {
  margin-top: 20px;
  text-align: center;
  color: #909399;
  font-size: 12px;
}

/* 猫头鹰样式 */
.owl {
  width: 211px;
  height: 108px;
  background: url('@/assets/images/猫头鹰.png') no-repeat;
  background-size: contain;
  position: absolute;
  top: -100px;
  left: 50%;
  transform: translateX(-50%);
}

.owl .hand {
  width: 34px;
  height: 34px;
  border-radius: 40px;
  background-color: #472d20;
  position: absolute;
  left: 12px;
  bottom: -8px;
  transform: scaleY(0.6);
  transition: 0.3s ease-out;
}

.owl .hand.hand-r {
  left: 170px;
}

.owl.password .hand {
  transform: translateX(42px) translateY(-15px) scale(0.7);
}

.owl.password .hand.hand-r {
  transform: translateX(-42px) translateY(-15px) scale(0.7);
}

.owl .arms {
  position: absolute;
  top: 58px;
  width: 100%;
  height: 41px;
  overflow: hidden;
}

.owl .arms .arm {
  width: 40px;
  height: 65px;
  position: absolute;
  left: 20px;
  top: 40px;
  background: url('@/assets/images/手.png') no-repeat;
  background-size: contain;
  transform: rotate(-20deg);
  transition: 0.3s ease-out;
}

.owl .arms .arm.arm-r {
  transform: rotate(20deg) scaleX(-1);
  left: 158px;
}

.owl.password .arms .arm {
  transform: translateY(-40px) translateX(40px) rotate(0deg);
}

.owl.password .arms .arm.arm-r {
  transform: translateY(-40px) translateX(-40px) rotate(0deg) scaleX(-1);
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-box {
    width: 95%;
    max-width: 360px;
  }
  
  .owl {
    width: 180px;
    height: 92px;
  }
  
  .owl .hand {
    width: 30px;
    height: 30px;
  }
  
  .owl .hand.hand-r {
    left: 145px;
  }
  
  .owl .arms .arm {
    width: 35px;
    height: 58px;
  }
  
  .owl .arms .arm.arm-r {
    left: 135px;
  }
}
</style> 